package org.dairy.farms.nandana.web.controller;

import org.dairy.farms.model.Farm;
import org.dairy.farms.model.Livestock;
import org.dairy.farms.nandana.core.NandanaUtils;
import org.dairy.farms.nandana.core.logic.FarmManagementLogic;
import org.dairy.farms.nandana.web.view.FarmExpenseView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * User: gduggira
 * Date: Jun 16, 2011
 * Time: 4:05:39 PM
 */
@Controller
public class FarmController {
    private static final Logger logger = LoggerFactory.getLogger(FarmController.class);
    @Autowired
    private FarmManagementLogic farmManagementLogic;
    @Autowired
    private NandanaUtils nandanaUtils;

    @RequestMapping(value = "/farms/listFarms", method = RequestMethod.GET)
    public void listFarms(Model model) {
        List<Farm> farmsList = farmManagementLogic.getAllFarms();
        model.addAttribute("farmsList", farmsList);
        //return "listFarms";
    }

    @RequestMapping(value = "farms/farmLivestockList", method = RequestMethod.GET)
    public void livestockList(@RequestParam(value = "id", required = true) Long farmId, Model model,
                              @RequestParam(value = "page", required = true) int page,
                              @RequestParam(value = "start", required = true) int start,
                              @RequestParam(value = "limit", required = true) int limit) {
        List<Livestock> farmLivestockList = farmManagementLogic.getLivestockOfFarm(farmId, page, start, limit);
        long livestockTotalCount = farmManagementLogic.getLivestockOfFarmCount(farmId);
        model.addAttribute("farmLivestockList", farmLivestockList);
        model.addAttribute("totalCount", livestockTotalCount);
    }

   // @RequestMapping(value = "farms/updateFarmExpense", method = RequestMethod.GET)
    public void updateFarmExpense(@RequestParam(value = "id", required = true) Long farmId, Model model,
                                  @RequestParam(value = "expenseTypeId", required = true) Long expenseTypeId,
                                  @RequestParam(value = "expenseNote", required = false) String expenseNote,
                                  @RequestParam(value = "amount", required = true) float amount,
                                  @RequestParam(value = "dateOfPayment") String dateOfPayment,
                                  @RequestParam(value = "farmExpenseId", required = false) Long farmExpenseId) {
        FarmExpenseView farmExpenseView = new FarmExpenseView();
        farmExpenseView.setAmount(amount);
        farmExpenseView.setExpenseNote(expenseNote);
        farmExpenseView.setExpenseTypeId(expenseTypeId);
        farmExpenseView.setFarmId(farmId);
        farmExpenseView.setFarmExpenseId(farmExpenseId);
        farmExpenseView.setDateOfPayment(nandanaUtils.getDate(dateOfPayment));
        Farm farm = farmManagementLogic.updateFarmExpense(farmExpenseView);
    }

    @RequestMapping(value = "farms/getFemaleLivestockList", method = RequestMethod.GET)
    public ModelAndView getFemaleLivestockList(@RequestParam(value = "id", required = true) Long farmId) {
        List<Livestock> farmLivestockList = farmManagementLogic.getFemaleLivestockOfFarm(farmId);
        long livestockTotalCount = farmManagementLogic.getFemaleLivestockOfFarmCount(farmId);
        ModelAndView modelAndView = new ModelAndView("farms/farmLivestockList");
        modelAndView.addObject("farmLivestockList", farmLivestockList);
        modelAndView.addObject("totalCount", farmLivestockList.size());
        return modelAndView;
    }

    @RequestMapping(value = "farms/getMaleLivestockList", method = RequestMethod.GET)
    public ModelAndView getMaleLivestockList(@RequestParam(value = "id", required = true) Long farmId) {
        List<Livestock> farmLivestockList = farmManagementLogic.getMaleLivestockOfFarm(farmId);
        long livestockTotalCount = farmManagementLogic.getMaleLivestockOfFarmCount(farmId);
        ModelAndView modelAndView = new ModelAndView("farms/farmLivestockList");
        modelAndView.addObject("farmLivestockList", farmLivestockList);
        modelAndView.addObject("totalCount", farmLivestockList.size());
        return modelAndView;
    }

    @RequestMapping(value = "farms/farmLivestockListCount", method = RequestMethod.GET)
    public void livestockListCount(Model model) {
        List<Farm> farmsList = farmManagementLogic.getUserFarmLivestockListCount();
        model.addAttribute("farmLivestockListCount", farmsList);
    }
}
